﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace AccidentCards
{
    class PersonCard
    {
        static public void CreatePersonCard(ref Int64 id, Int64 accidentId)
        {
            while (!dbServices.TestDbAccess())
            {
                DialogResult mesRslt = MessageBox.Show("Соединение потеряно.\nЧтобы повторить попытку подключения нажмите 'Повторить' или 'Отмена' для отмены действия.", "Ошибка", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
                if (mesRslt == DialogResult.Retry)
                {
                    Application.DoEvents();
                }
                else
                    if (mesRslt == DialogResult.Cancel)
                    {
                        return;
                    }
            }
            
            try
            {

                SqlCommand cmd = new SqlCommand("CreatePersonCard", dbServices.SqlConn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@id", SqlDbType.BigInt, 4);
                cmd.Parameters["@id"].Value = accidentId;

                cmd.Connection.Open();
                Object o = cmd.ExecuteScalar();
                id = Convert.ToInt64(o);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Ошибка, при создании нового события. Код 1: " + ex.ToString());
                return;

            }
            catch
            {
                MessageBox.Show("Ошибка, при создании нового события. Код 2.");
                return;

            }
        }

        public static int UpdateCard(
                                            long id,
                                            int accidentMemberType,
	                                        int vehicleNumber,
	                                        int escape,
	                                        String fullName,
	                                        String driverLicenceId,
	                                        int gender,
                                            DateTime birthday,
	                                        int driverExperience,
	                                        int drivingTime,
	                                        int socialType,
	                                        int consequencesType,
	                                        int nation,
	                                        String livingPlace,
	                                        int violationsOfTrafficLaw )
        {
            dbServices.Connect();
            var sqlCmd = new SqlCommand("UpdatePersonCard", dbServices.SqlConn);

            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@id", SqlDbType.BigInt, 4);	
	        sqlCmd.Parameters.Add("@accidentMemberType", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@vehicleNumber", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@escape", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@fullName", SqlDbType.NVarChar, 100);
	        sqlCmd.Parameters.Add("@driverLicenceId", SqlDbType.NVarChar, 50);
	        sqlCmd.Parameters.Add("@gender", SqlDbType.Int, 4);
            sqlCmd.Parameters.Add("@birthday", SqlDbType.DateTime2);
	        sqlCmd.Parameters.Add("@driverExperience", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@drivingTime", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@socialType", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@consequencesType", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@nation", SqlDbType.Int, 4);
	        sqlCmd.Parameters.Add("@livingPlace", SqlDbType.NVarChar, 100);
	        sqlCmd.Parameters.Add("@violationsOfTrafficLaw", SqlDbType.Int, 4);           

            sqlCmd.Parameters["@id"].Value = id;
            sqlCmd.Parameters["@accidentMemberType"].Value = accidentMemberType;
            sqlCmd.Parameters["@vehicleNumber"].Value = vehicleNumber;
            sqlCmd.Parameters["@escape"].Value = escape;
            sqlCmd.Parameters["@fullName"].Value = fullName;
            sqlCmd.Parameters["@driverLicenceId"].Value = driverLicenceId;
            sqlCmd.Parameters["@gender"].Value = gender;
            sqlCmd.Parameters["@birthday"].Value = birthday;
            sqlCmd.Parameters["@driverExperience"].Value = driverExperience;
            sqlCmd.Parameters["@drivingTime"].Value = drivingTime;
            sqlCmd.Parameters["@socialType"].Value = socialType;
            sqlCmd.Parameters["@consequencesType"].Value = consequencesType;
            sqlCmd.Parameters["@nation"].Value = nation;
            sqlCmd.Parameters["@livingPlace"].Value = livingPlace;
            sqlCmd.Parameters["@violationsOfTrafficLaw"].Value = violationsOfTrafficLaw;            

            try
            {
                sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return -1;
            }
            finally
            {
                sqlCmd.Dispose();
                dbServices.Close();
            }
            return 0;
        }

        static public DataTable GetAllPerson(Int64 id)
        {

            DataTable rslt = new DataTable();

            dbServices.Connect();
            var sqlCmd = new SqlCommand("GetPersonList", dbServices.SqlConn);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@id", SqlDbType.BigInt, 4);
            sqlCmd.Parameters["@id"].Value = id;

            try
            {
                rslt.Load(sqlCmd.ExecuteReader());
            }            
            catch
            {
                return null;
            }
            return rslt;
        }

        public static DataTable GetInfoById(long vhId)
        {
            var result = new DataTable("PersonCardInfo");
            dbServices.Connect();
            var sqlAdapter = new SqlDataAdapter("GetPersonCardInfoByID", dbServices.SqlConn);
            sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlAdapter.SelectCommand.Parameters.Add("@PersonCardID", SqlDbType.BigInt, 4);
            sqlAdapter.SelectCommand.Parameters["@PersonCardID"].Value = vhId;
            try
            {
                sqlAdapter.Fill(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sqlAdapter.Dispose();
                dbServices.Close();
            }
            return result;
        }
    }
}
